﻿using CommunicationProtocol.dto;
using CommunicationProtocol.dto.command.client;
using CommunicationProtocol.dto.game;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace server.gameLogic
{
    public class GameDataContext
    {
        private static GameDataContext Instance = new GameDataContext();

        public List<StaticPlant> buyablePlants { get; }

        public List<List<Field>> fields { get; }

        Random random;

        public static GameDataContext get()
        {
            return Instance;
        }

        /**
         * 获取植物的冷却剩余时间
         */
        internal int getPlantCoolingDownRemainingSeconds(int objectId, string currentUserId, out string currentOwnerId)
        {
            StaticPlant v = buyablePlants.Find(plant => plant.ObjectId == objectId);

            if (v is null)
            {
                Console.WriteLine($"未找到id = {objectId} 的植物");
                currentOwnerId = null;
                return 0;
            }

            return v.setOwnerIdIfCoolingTimeIsZero(currentUserId, out currentOwnerId);
        }

        internal DynamicPlant plantANewPlant(PlantAPlant plantAPlant, string userId)
        {
            StaticPlant v = buyablePlants.Find(plant => plant.ObjectId == plantAPlant.targetPlantId);

            if (v is null)
            {
                Console.WriteLine($"未找到id = {plantAPlant.targetPlantId} 的植物");

                return null;
            }

            if (v.createAPlant(userId))
            {
                DynamicPlant newPlant = PlantFactory.fromObjectType(
                    (short) random.Next(10000),
                    v.ObjectType,
                    (byte) plantAPlant.column,
                    (byte) plantAPlant.row
                );

                Field field = fields[plantAPlant.column][plantAPlant.row];
                field.plant(newPlant);
                return newPlant;
            }

            return null;
        }

        public GameDataContext()
        {
            random = new Random();

            buyablePlants = new List<StaticPlant>();

            // 豌豆射手
            buyablePlants.Add(new StaticPlant(0x1234 /*((short)random.Next(60000))*/, 1, 0, 0,0));
            // 向日葵
            buyablePlants.Add(new StaticPlant(0x7890 /*((short)random.Next())*/, 2, 0, 0,0));
            fields = new List<List<Field>>();
            for (byte i = 0; i < 9; i++)
            {
                List<Field> column = new List<Field>();
                for (byte j = 0; j < 5; j++)
                {
                    column.Add( new Land(
                        (short) random.Next(10000),
                        0,
                        i,
                        j
                    ));
                }
                
                fields.Add(column);
            }
        }

        public List<DynamicPlant> allPlantsOnMap()
        {
            var result = new List<DynamicPlant>();
            foreach (List<Field> column in fields)
            {
                foreach (Field field in column)
                {
                    List<DynamicPlant> plants =  field.allPlants();
                    result.AddRange(plants);
                }
            }

            return result;
        }
    }
}